function City(params) {
    var that = this
    this.params = params;
    this.city = {}


    this.cityM = function() {
        for (var i in this.city) {
            var div = document.createElement('li');
            $(div).attr('data-code', i)
            $(div).html(this.city[i].name)
            $(this.params.ele + ' .sheng .option ul').append(div)
            $(div).click(function(e) {
                $(this).parents('.sheng').find('.required').val($(this).text())
                $(this).parents('.sheng .option').slideUp()
                that.area($(this).attr('data-code'))
                $(this).parents(that.params.ele).find('.shi .required').val(that.city[$(this).attr('data-code')].list[0])

            })
        }
    }.bind(this)


    this.area = function(code) {
        var data = this.city[code].list
        $(this.params.ele + ' .shi .option ul').html('')
        for (var i in data) {
            var div = document.createElement('li');
            $(div).html(data[i])
            $(this.params.ele + ' .shi .option ul').append(div)
            $(div).click(function(e) {
                $(this).parents('.shi').find('.required').val($(this).text())
                $(this).parents('.shi .option').slideUp()
            })
        }
    }.bind(this)


    var index = 0;
    $('.worm_map ul li[data-code] .info .citylist ol').html('')

    for (var i in this.params.data) {
        var key = i.substr(0, 2)
        if (index != key) {
            try {
                setCityCode(this.params.data[i], key)
            } catch (err) {

            }
            this.city[key] = {
                name: this.params.data[i],
                list: []
            };
            index = key
            $('.worm_map ul li[data-code="' + key + '"] .info .cityname').html(this.params.data[i])

        } else {
            if ((/^.*市$/.test(this.params.data[i])) || (key == '11' || key == '12' || key == '31' || key == '50' || key == '81' || key == '82')) {
                this.city[key].list.push(this.params.data[i]);
                var li = document.createElement('li');
                $(li).html('<a>' + this.params.data[i] + '</a>')
                $(li).click(function() {
                    $('.address_city .sheng .required').val($(this).parents('.citycont').find('.cityname').text())
                    City1.area($(this).parents('li[data-code]').attr('data-code'))
                    $('.address_city .shi .required').val($(this).text())
                    $("html,body").animate({ "scrollTop": 1000 });
                })
                $('.worm_map ul li[data-code="' + key + '"] .info .citylist ol').append(li)
            }
        }
    }
    this.cityM()





}


var City1 = null
$.getJSON("../js/city.json", function(data) {

    City1 = new City({
        option: 'city',
        ele: '.address_city',
        data: data,
    });
    new City({
        option: 'city',
        ele: '.address_city02',
        data: data,
    });
    new City({
        option: 'city',
        ele: '.address_city03',
        data: data,
    });

})